$(function() {
    var thiswinurl = window.location.href,
        hqTaskIdStr = "hqTaskId",
        hqTaskIdIdx = thiswinurl.indexOf(hqTaskIdStr),
        hqTaskIdVal = (hqTaskIdIdx == -1) ? "" : thiswinurl.substring(hqTaskIdIdx + (hqTaskIdStr.length + 1));

    $.ajax({
        type: "post",
        dataType: "json",
        data: {
            "hqTaskId": hqTaskIdVal
        },
        url: DI.queryHqTaskProgress,
        success: function(data) {
            if (data.retFlag == 1) {
            	
                mainCtrl.hqTaskId = hqTaskIdVal;
                mainCtrl.cateName = data.data.cateName;
                mainCtrl.month = data.data.month;
                mainCtrl.taskTopic = data.data.taskTopic;
                mainCtrl.currentSaleNum = data.data.currentSaleNum;
                mainCtrl.targetSaleNum = data.data.targetSaleNum;
                mainCtrl.currentSaleAmount = data.data.currentSaleAmount.toFixed(2);
                mainCtrl.cateSaleAmount = data.data.cateSaleAmount.toFixed(2);
//                mainCtrl.saleNumRate = data.data.saleNumRate;
                mainCtrl.saleAmountRate = data.data.saleAmountRate;
            
                mainCtrl.rate = data.data.rate;
                
                $(".amountDiv .bar div").css("width", (mainCtrl.saleAmountRate > 100 ? 100 : mainCtrl.saleAmountRate) + "%");
//            	$(".numDiv .bar div").css("width", (parseInt($("#hqRate").html()) > 100 ? 100 : parseInt($("#hqRate").html())) + "%");
            } else {
                console.log("获取数据失败了哟>>>" + data.errorMessage)
            }
        },
        error: function(err) {
            console.log(err);
        }
    });

    var opts22 = {
        targetId: "taskTable22",
        templateId: "tabletask22",
        settings: {
            searchParam: {
                "hqTaskId": hqTaskIdVal,
                "queryParam": $(".u-ipt").val()
            },
            url: DI.queryAreaTaskProgress,
            ajaxType: "json",
            sourceInterface: {
                INum: "totalSize",
                IData: "records"
            },
            paginationIndexName: {
                "peerpageRows": "pageSize",
                "currentPage": "pageNo"
            }
        },
        customFuncs: {
        	detailModel: function(row) {
                $("#popup_distribution").show();
                $("#load-assign").show();
                popCtrl.assignList = [];
                popCtrl.searchcode = "";
                $(".search-code .err-box").hide();
                popCtrl.hqTaskId = mainCtrl.hqTaskId;
                $.ajax({
                    type: "post",
                    dataType: "json",
                    data: {
                        "disCode": row.disCode,
                        "hqTaskId": popCtrl.hqTaskId
                    },
                    url: DI.queryModelProgress,
                    success: function(data) {
                        $("#load-assign").hide();
                        if (data.retFlag == 1) {
                            popCtrl.assignList = data.data;
                        } else {
                        	PLATFORM.showTips("error", data.errorMessage);
                        }
                    },
                    error: function(err) {
                        console.log(err);
                    }
                });
            }
        }
    };
    taskTable22 = SwiftUI.swiftTable.getInstance(opts22);
    
    var mainCtrl = avalon.define({
        $id: "mainController",
        taskTopic: "",
        month: "",
        rate: "",
        hqTaskId: "",
        cateName: "",
        saleNumRate: "",
        saleAmountRate: "",
        currentSaleNum: "",
        targetSaleNum: "",
        currentSaleAmount: "",
        cateSaleAmount: "",
        categoryList: [],
        isimppercent: false,
        isimpcomm: false,
        tabIdx: 0,
        
        selTab: function() {
            mainCtrl.tabIdx = $(this).index();
        },
        exportTask: function() {
        	window.location.href = DI.exportTaskProgress + "?hqTaskId=" + mainCtrl.hqTaskId;
        }, 
        queryTask: function() {
        	var searchParam = {
    			"hqTaskId": hqTaskIdVal,
                "queryParam": $(".u-ipt").val()
            }
        	taskTable22.search(searchParam);
        },
        nextComm: function() {
            mainCtrl.tabIdx = 1;
        },
        prevPercent: function() {
            mainCtrl.tabIdx = 0;
        }
    });

    var popCtrl = avalon.define({
        $id: "popController",
        assignList: [],
        searchcode: "",
        delId: "",
        delcommName: "",
        modelId: "",
        hqTaskId: "",
        targetNum: 0,
        uploadFile: function() {
            if ($("#upladFile").val() == "") {
                return;
            }
            $("#load-import").show();
            $("#form_file").ajaxSubmit({
                type: "post",
                dataType: "json",
                url: DI.addModel,
                success: function(data) {
                    $("#load-import").hide();
                    if (data.retFlag == 1) {
                        $("#popup_import").hide();
                        taskTable22.searchParam.pageNo = "1";
                        taskTable22.refresh();
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                    $("#upladFile").after($("#upladFile").clone().val(""));
                    $("#upladFile").remove();
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    console.log("ajax error");
                    console.log(XMLHttpRequest);
                }
            });
        }
    });

    avalon.scan();
});
